package fr.afcepf.atod.gestionVP.dao;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import fr.afcepf.atod.gestionVP.data.Client;
import fr.afcepf.atod.gestionVP.data.Commercial;
import fr.afcepf.atod.gestionVP.data.Vente;

@Component("DaoVenteImpl")  //pour une prise en charge par Spring
@Transactional
public class DaoVenteImpl implements IDaoVente {

	//@PersistenceContext (d'origine Ejb3)
	//permet d'initialiser entityManager 
	//en fonction de META-INF/persitence.xml 
	@PersistenceContext(unitName="Gestion Vente Paiement")
	private EntityManager em;

//****************************************************************************************
//		Methode provenant de IDao
//****************************************************************************************
		
	
	public void createVente(Vente vente) {
		em.persist(vente);
	}
	
	public void deleteVente(Vente vente) {
		em.remove(vente);
	}

	
	public void updateVente(Vente vente) {
		em.merge(vente);
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Vente> getAllVente(int id) {
		Query query = em.createQuery("SELECT ve FROM Vente as ve WHERE ve.id =:param");
		query.setParameter("param", id);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public Collection<Client> getClientByCom(int idCom) {
		Query query = em.createQuery("select com.clients from Commercial com where com.id=?");
		query.setParameter(1, idCom);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public Collection<Commercial> getAllCom() {
		Query query = em.createQuery("select com from Commercial com");
		return query.getResultList();
	}
	
}
